import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class RemoveEmptyTag {

	/**
	 * List all the files and folders from a directory
	 * 
	 * @param directoryName
	 *            to be listed
	 */
	public String filename;

	public void listFilesAndFolders(String directoryName) throws SAXException,
			IOException, TransformerException {

		File directory = new File(directoryName);

		// get all the files from a directory
		File[] fList = directory.listFiles();

		for (File file : fList) {

		}
	}

	/**
	 * List all the files under a directory
	 * 
	 * @param directoryName
	 *            to be listed
	 * @throws TransformerException
	 * @throws IOException
	 * @throws SAXException
	 */
	public void listFiles(String directoryName) throws SAXException,
			IOException, TransformerException {

		File directory = new File(directoryName);

		// get all the files from a directory
		File[] fList = directory.listFiles();
		int ii = 0;
		for (File file : fList) {
			if (file.isFile()) {
				System.out.println(++ii);
				filename = directoryName + File.separator + file.getName();
				System.out.println();
				data(filename);
			}
		}
	}

	/**
	 * List all the folder under a directory
	 * 
	 * @param directoryName
	 *            to be listed
	 */
	public void listFolders(String directoryName) {

		File directory = new File(directoryName);

		// get all the files from a directory
		File[] fList = directory.listFiles();

		for (File file : fList) {
			if (file.isDirectory()) {
				System.out.println(file.getName());
			}
		}
	}

	/**
	 * List all files from a directory and its subdirectories
	 * 
	 * @param directoryName
	 *            to be listed
	 */
	public void listFilesAndFilesSubDirectories(String directoryName) {

		File directory = new File(directoryName);

		// get all the files from a directory
		File[] fList = directory.listFiles();

		for (File file : fList) {
			if (file.isFile()) {
				System.out.println(file.getAbsolutePath());
			} else if (file.isDirectory()) {
				listFilesAndFilesSubDirectories(file.getAbsolutePath());
			}
		}
	}

	public static void main(String[] args) throws SAXException, IOException,
			TransformerException {

		RemoveEmptyTag listFilesUtil = new RemoveEmptyTag();

		final String directoryLinuxMac = "/Users/loiane/test";

		// Windows directory example
		// final String directoryWindows
		// ="C:/Users/QA Admin/Documents/bat/dell cases/search_suite/sample_SPS/New folder/";
		final String directoryWindows = args[0];
		System.out.println(directoryWindows);
		listFilesUtil.listFiles(directoryWindows);

	}

	public void data(String fname) throws SAXException, IOException,
			TransformerException {
		BufferedReader br = null;
		File XmlFile = null;
		try {
			XmlFile = new File(fname);
			br = new BufferedReader(new FileReader(XmlFile));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.getMessage();
			e.printStackTrace();
			System.exit(0);
			System.exit(0);
		}
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = null;
		try {
			dBuilder = dbFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.getMessage();
			e.printStackTrace();
			System.exit(0);
		}

		//int flag = 0;// to delete Nde
		/*String[] tag = { "XTranLocalCompanyName", "XTranLocallangFirstName",
				"XTranLocallangMiddleName", "XTranLocallangLastName",
				"XTranKatakanaFirstName", "XTranKatakanaLastName",
				"XTranLocallangOrgName", "XTranKatakanaOrgName",
				"XTranKatakanaBranchName", "XTranLocallangAddrLineOne",
				"XTranLocallangAddrLineTwo", "XTranLocallangCity",
				"XTranLocallangState", "XTranLocallangZipcode",
				"XTranLocallangCountry", "XTranKatakanaAddrLineOne",
				"XTranKatakanaAddrLineTwo", "XTranKatakanaCity",
				"XTranKatakanaState", "XTranKatakanaZipcode",
				"XTranKatakanaCountry" };*/
		Document doc = dBuilder.parse(XmlFile);
		/*if (flag == 0) {
			for (int k = 0; k < tag.length; k++) {
				Element element = (Element) doc.getElementsByTagName(tag[k])
						.item(0);
				if (element != null)
					element.getParentNode().removeChild(element);
				
			}
		}
*/
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = null;
		try {
			transformer = tFactory.newTransformer(new StreamSource(
					new RemoveEmptyTag().getClass().getResourceAsStream(
							"/remove.xsl")));

		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		transformer.setOutputProperty(OutputKeys.INDENT, "yes");
		DOMSource source = new DOMSource(doc);
		System.out.println(source);
		StreamResult result = new StreamResult(new File(fname));
		transformer.transform(source, result);

	}
}
